<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="assets/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/right.css" rel="stylesheet">
    <script src="assets/jquery-3.5.1.min.js"></script>
    <script src="assets/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
    <script src="assets/vue.min-v2.5.16.js"></script>
    <script src="assets/vue-router.min-2.7.0.js"></script>
    <script src="assets/axios.min.js"></script>
    <script src="assets/date_picker.js"></script>
</head>
<body>
    <div id="app" class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="row">
                    <div class="col-md-12" style="text-align: center; font-weight: bold; font-size: 18px; height: 80px; line-height: 80px;">
                        {{title}}
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <label>楼号</label>
                        <input type="text" class="form-control" v-model="building.numbers">
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <label>单元</label>
                        <input type="text" class="form-control" v-model="building.uints">
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <label>备注</label>
                        <input type="text" class="form-control" v-model="building.remarks">
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <button class="btn btn-primary" @click="doSave">保存</button>
                        <button class="btn btn-default" @click="doCancel">取消</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                title: null,
                userId: null,
                building:{id: 0,numbers: null,uints: null,remarks:null}
            },
            methods: {
                doSave(){
                    if(this.userId != null){ //修改
                        var uid = localStorage.getItem("uid");
                        axios.post('http://localhost:8080/building/update?uid='+uid,this.building).then(response =>{
                            if(response.data.code==200){
                                alert("修改成功");
                                window.parent.main_right.location.href="building_list.html";
                            }else{
                                alert("修改失败");
                                window.parent.main_right.location.href="building_list.html";
                            }
                        });
                    } else { //保存
                        //通过axios发送请求
                        var uid = localStorage.getItem("uid");
                        axios.post("http://localhost:8080/building/add?uid="+uid,this.building).then(re =>{
                            if(re.data.code==200){
                                //跳转到用户列表页面
                                alert("添加成功");
                                window.parent.main_right.location.href="building_list.html";
                            }else {
                                alert("添加失败");
                            }
                        });
                    }
                },
                doCancel(){
                   history.go(-1);
                }
            },
            created: function () {
                var uid = localStorage.getItem("uid");
                var url = window.location.href;
                if(url.indexOf('id') != -1){
                    this.userId = url.substring(url.indexOf('=')+1);
                }
                // console.log("接收传参-用户ID："+this.userId);
                if(this.userId != null){
                    this.title = "修改楼宇";
                    var url='http://localhost:8080/building/selectone?id='+this.userId+'&uid='+uid;
                    axios.get(url).then(response =>{
                        this.building=response.data.data;
                    });
                } else {
                    this.title = "添加楼宇";
                }
            }
        });
    </script>
</body>
</html>